package system.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import system.bean.MemberRoleRelation;
import system.dao.DaoInterFace;
import system.dao.templete.DbDataOperate;
import util.ObjectRelationMappingUtil;

public class MemberRoleRelationDao extends DbDataOperate implements
		DaoInterFace {
	@Override
	protected Object doFillFullBean(ResultSet rs) throws SQLException {
		MemberRoleRelation memberRoleRelation = new MemberRoleRelation();
		ObjectRelationMappingUtil.fillinBO(memberRoleRelation, rs);
		return memberRoleRelation;
	}

	@Override
	protected Object doFillListBean(ResultSet rs) throws SQLException {
		
		return doFillFullBean(rs);
	}

	@Override
	public boolean delete(Object bean) {
		MemberRoleRelation tempBean = (MemberRoleRelation)bean;
		String sql       = "delete from " + tempBean.getTableName() + " where memberId = "+ tempBean.getMemberId()+ " and roleId = "+tempBean.getRoleId();
		String exception = tempBean.getClass()+" Function delete():";
		return super.executeSql(sql, exception);
	}

	@Override
	public boolean insert(Object bean) {
		String sql       = ObjectRelationMappingUtil.getObjectInsertSQL((MemberRoleRelation)bean);
		String exception = ((MemberRoleRelation)bean).getClass()+" Function insert():";
		return super.insert(sql,exception);
	}

	@Override
	public boolean update(Object bean) {
		return false;
	}

	public boolean deleteRelationByMemberId(int memberId){
		String sql       = "delete from " + MemberRoleRelation.tableName + " where memberId = "+ memberId;
		String exception = this.getClass()+":deleteRelationByMemberId(int memberId) has error!";
		return super.executeSql(sql, exception);
	}
	
	public ArrayList<Object> getListByRoleId(int roleId){
		String sql       = "select * from " + MemberRoleRelation.tableName + " where roleId = " + roleId;
		String exception = this.getClass()+":getMemberListByRoleId(int roleId) has error!";
		return super.getList(sql, exception);
	}
	
	public ArrayList<Object> getListByMemberId(int memberId){
		String sql       = "select * from " + MemberRoleRelation.tableName + " where memberId = " + memberId;
		String exception = this.getClass()+":getMemberListByRoleId(int roleId) has error!";
		return super.getList(sql, exception);
	}
}
